package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Wallet;
import com.neusoft.elmboot.po.WalletTransaction;
import com.neusoft.elmboot.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;

@RestController
@RequestMapping("/api/wallet")
public class WalletController {

    @Autowired
    private WalletService walletService;

    @GetMapping("/info")
    public Wallet getWalletByUserId(@RequestParam String userId) {
        return walletService.getWalletByUserId(userId);
    }

    @PostMapping("/recharge")
    public ResponseEntity<String> recharge(@RequestParam String userId,
                                           @RequestParam BigDecimal amount,
                                           @RequestParam String rule) {
        return walletService.recharge(userId, amount, rule)
                ? ResponseEntity.ok("充值成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("充值失败");
    }

    @PostMapping("/withdraw")
    public ResponseEntity<String> withdraw(@RequestParam String userId,
                                           @RequestParam BigDecimal amount) {
        return walletService.withdraw(userId, amount)
                ? ResponseEntity.ok("提现成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("提现失败");
    }

    @PostMapping("/pay")
    public ResponseEntity<String> pay(@RequestParam String fromUserId,
                                      @RequestParam String toUserId,
                                      @RequestParam BigDecimal amount) {
        return walletService.pay(fromUserId, toUserId, amount)
                ? ResponseEntity.ok("支付成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("支付失败");
    }

    @PostMapping("/freeze")
    public ResponseEntity<String> freeze(@RequestParam String userId,
                                         @RequestParam BigDecimal amount) {
        return walletService.freeze(userId, amount)
                ? ResponseEntity.ok("冻结成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("冻结失败");
    }

    @PostMapping("/unfreeze")
    public ResponseEntity<String> unfreeze(@RequestParam String userId,
                                           @RequestParam BigDecimal amount) {
        return walletService.unfreeze(userId, amount)
                ? ResponseEntity.ok("解冻成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("解冻失败");
    }

    @PostMapping("/creditLimit")
    public ResponseEntity<String> setCreditLimit(@RequestParam String userId,
                                                 @RequestParam BigDecimal limit) {
        return walletService.setCreditLimit(userId, limit)
                ? ResponseEntity.ok("设置透支额度成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("设置透支额度失败");
    }

    @PostMapping("/transaction")
    public ResponseEntity<String> recordTransaction(@RequestBody WalletTransaction transaction) {
        return walletService.recordTransaction(transaction)
                ? ResponseEntity.ok("交易记录成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("交易记录失败");
    }

    @PostMapping("/confirm")
    public ResponseEntity<String> confirmReceive(@RequestParam String userId,
                                                 @RequestParam Integer orderId) {
        return walletService.confirmReceive(userId, orderId)
                ? ResponseEntity.ok("确认收货成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("确认收货失败");
    }

    @GetMapping("/transaction/{orderId}")
    public ResponseEntity<WalletTransaction> checkTransactionStatus(@PathVariable Integer orderId) {
        WalletTransaction transaction = walletService.checkTransactionStatus(orderId);
        return transaction != null
                ? ResponseEntity.ok(transaction)
                : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }
}